package com.example.demo.entity.base;

import java.io.Serializable;
import java.util.List;

import com.alibaba.fastjson.JSONObject;

/**
 * @title:	ResponseEntity
 * @author:	许子文
 * @date:	2019年9月14日 
 * @descirption: 有泛型的返回实体
 */
public class ResponseEntity<T> implements Serializable{
	private static final long serialVersionUID = 7495718975643567968L;

	private int code;
	
	private String msg;
	
	private T retdata;
	
	private List<T> retlist;

	public ResponseEntity(int code, String msg, T retdata, List<T> retlist) {
		super();
		this.code = code;
		this.msg = msg;
		this.retdata = retdata;
		this.retlist = retlist;
	}

	public ResponseEntity() {
		super();
	}
	
	public ResponseEntity<T> setList(List<T> listdata){
		this.code = 200;
		this.msg = "SUCCESS";
		this.retlist = listdata;
		return this;
	}
	
	public ResponseEntity<T> setObject(T retdata) {
		this.code = 200;
		this.msg = "SUCCESS";
		this.retdata = retdata;
		return this;
	}
	
	public ResponseEntity<T> setSuccess(T retdata,String msg) {
		this.code = 200;
		this.msg = msg;
		this.retdata = retdata;
		return this;
	}
	
	public ResponseEntity<T> setSuccess(String msg) {
		this.code = 200;
		this.msg = msg;
		return this;
	}
	
	public ResponseEntity<T> setSuccess(String msg,int code) {
		this.code = code;
		this.msg = msg;
		return this;
	}
	
	public ResponseEntity<T> setFail(String errorMsg) {
		this.code = 500;
		this.msg = errorMsg;
		return this;
	}
	
	public ResponseEntity<T> setFail(String errorMsg,int errorCode) {
		this.code = errorCode;
		this.msg = errorMsg;
		return this;
	}
	
	public ResponseEntity<T> setFail(int errorCode,String errorMsg,T retdata){
		this.code = errorCode;
		this.msg = errorMsg;
		this.retdata = retdata;
		return this;
	}
	
	public ResponseEntity<T> setFail(int errorCode,String errorMsg,List<T> retlist){
		this.code = errorCode;
		this.msg = errorMsg;
		this.retlist = retlist;
		return this;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getRetdata() {
		return retdata;
	}

	public void setRetdata(T retdata) {
		this.retdata = retdata;
	}

	public List<T> getRetlist() {
		return retlist;
	}

	public void setRetlist(List<T> retlist) {
		this.retlist = retlist;
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return JSONObject.toJSONString(this);
	}
}
